package android.support.v7.appcompat;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.Button;

/**
 * Created by HSH on 2016/12/21.
 */

public class AlertDialogPositiveButtonHack {
    public static void hackPositiveButton(final AlertDialog dialog, String text, final DialogInterface.OnClickListener listener) {
        if (dialog == null) {
            return;
        }
        Window mWindow = dialog.getWindow();
        View parentPanel = mWindow.findViewById(R.id.parentPanel);
        View defaultButtonPanel = parentPanel.findViewById(R.id.buttonPanel);

        Button mButtonPositive = (Button) defaultButtonPanel.findViewById(android.R.id.button1);

        if (mButtonPositive != null) {
            mButtonPositive.setText(text);
            mButtonPositive.setVisibility(View.VISIBLE);
            mButtonPositive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                }
            });
        }
    }
}
